/**
 * @author mili
 */

/**
 * Constant
 */
var CONSTANT_INCIDENT = 1;

// var
var txt, parsedObject, dataType;

// either copy the json file into this txt variable
// or use filereader to read json file into variable
// for use

// parse json file into parsedObject for use
//$.getJSON("incident.json", function(data){
	//parsedObject = JSON.parse(data);
//});
//parsedObject = JSON.parse(Fixedtxt);

/**
 * parse the json file
 */
function parse(txt, type){
	//parseObject = JSON.parse(Fixedtxt);
	$.getJSON("http://localhost/live-syd-traffic/server_program/server.php?action=getjson&field=&id=incident.json", function(data){
		parsedObject = JSON.parse(data);
		document.getElementById("numIncidents").innerHTML=parsedObject;
	});
	dataType = type;
	return parsedObject;
}

// call to check if parsed
function isParsed(){
	if(typeof parseObject === 'undefined'){
		return false;
	}
	return true;
}

// get the right object of the json file
function getRights(){
	if(!isParsed()){
		return null;
	}
	return parseObject.rights;
}

// get the copyright of the rights object
function getCopyRight(){
	if(typeof getRights() === 'undefined'){
		return null;
	}
	return parsedObject.rights.copyright;
}

// get the license of the rights object
function getLicence(){
	if(typeof getRights() === 'undefined'){
		return null;
	}
	return parsedObject.rights.licence;
}

// get the layerName of the json file
function getLayerName(){
	if(!isParsed()){
		return null;
	}
	return parsedObject.layerName;
}

// get the last published time of the json file
function getLastPublishedTime(){
	if(!isParsed()){
		return 0;
	}
	return parsedObject.lastPublished;
}

/**
 * Below are simple/generic access object methods:
 * Generic attributes:
 * 		Type
 * 		id
 * 		geometry
 * 		properties --- different in each json files.
 */
// return an array of features
function getFeatures(){
	if(!isParsed()){
		return null;
	}
	return parsedObject.features;
}

// return an feature based on index
function getFeature_index(feature_index){
	if(typeof parsedObject.features === 'undefined'){
		return null;
	}
	if(feature_index < 0 && feature_index >= parsedObject.features.length){
		return null;
	}
	return parsedObject.features[feature_index];
}

// return an feature based on id
function getFeature_id(feature_id){
	
}

// return the type of the geometry
function getGeometryType_id(feature_id){
	
}
function getGeometryType_index(feature_index){
	
}

// return latLng of a feature id
function getLatLng_id(feature_id){
	if(typeof getFeatures() === 'undefined'){
		return null;
	}
	
}

// return LatLng of a feature id
function getLatLng_index(feature, feature_index){
	var coordinates = feature[feature_index].geometry.coordinates;
	var newLatLng = new google.maps.LatLng(coordinates[1], coordinates[0]);
	return newLatLng;
}

/**
 * Below is the methods for accessing the properties
 * of feature data
 */
// return the Display Name of feature
function getDisplayName_index(){
	switch(type){
		case CONSTAHT_INCIDENT:
			return getFeature_index().displayName();
		default:
			return null;
	}
}
